package cn.zh.sql_session.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {SlaveDataSourceConfig.MAPPER_PACKAGE}, sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveDataSourceConfig {

    static final String  MAPPER_PACKAGE = "cn.zh.sql_session.mapper.slave";
    static final String  DOMAIN_PACKAGE = "cn.zh.sql_session.domain.entity.master";
    static final String  MAPPER_LOCATION = "classpath:slave/*.xml";

//    @Bean(name = "slaveTransactionManager")
//    public DataSourceTransactionManager slaveTransactionManager() {
//        return new DataSourceTransactionManager(slaveDataSource());
//    }

    @Bean(name = "slaveDataSource")
    @Qualifier("slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "slaveSqlSessionFactory")
    public SqlSessionFactory slaveSqlSessionFactory() throws Exception {

        // Mybatis 需要使用 SqlSessionFactoryBean
//        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

        // Mybatis-Plus 需要使用 MybatisSqlSessionFactoryBean
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        // 数据源设置
        sessionFactory.setDataSource(slaveDataSource());
        // 类别名
        sessionFactory.setTypeAliasesPackage(SlaveDataSourceConfig.DOMAIN_PACKAGE);
        // xml路径
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(SlaveDataSourceConfig.MAPPER_LOCATION);
        sessionFactory.setMapperLocations(resources);
        return sessionFactory.getObject();
    }

    @Bean("slaveSqlSessionTemplate")
    public SqlSessionTemplate slaveSqlSessionTemplate(
            @Qualifier("slaveSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    @Bean("salveTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("slaveDataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


}